﻿//------------------------------------------------------------
// Game Framework
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
// Homepage: https://gameframework.cn/
// Feedback: mailto:ellan@gameframework.cn
//------------------------------------------------------------

using System;
using System.Collections.Generic;
using GameFramework.Event;
using UnityEngine;
using UnityGameFramework.Runtime;
using ProcedureOwner = GameFramework.Fsm.IFsm<GameFramework.Procedure.IProcedureManager>;

namespace Cheng
{
    public class ProcedureMain : ProcedureBase
    {
        private const float GameOverDelayedSeconds = 2f;

        private readonly Dictionary<GameMode, GameBase> m_Games = new Dictionary<GameMode, GameBase>();
        private GameBase m_CurrentGame = null;
        private string ChangeSceneName = null;
        private string LoadSceneName = null;
        private string battleName = null;
        //private float m_GotoMenuDelaySeconds = 0f;

        public override bool UseNativeDialog
        {
            get
            {
                return false;
            }
        }

        

        protected override void OnInit(ProcedureOwner procedureOwner)
        {
            base.OnInit(procedureOwner);
           
            m_Games.Add(GameMode.Survival, new SurvivalGame());
            //m_Games.Add(GameMode.First, new GameFirst());
        }

        protected override void OnDestroy(ProcedureOwner procedureOwner)
        {
            base.OnDestroy(procedureOwner);

            m_Games.Clear();
        }

        protected override void OnEnter(ProcedureOwner procedureOwner)
        {
            base.OnEnter(procedureOwner);
            GameEntry.Event.Subscribe(CustomBaseEventArgs.EventId, OnChangeScene);
            ChangeSceneName = null;
            GameMode gameMode = (GameMode)procedureOwner.GetData<VarByte>("GameMode").Value;
            m_CurrentGame = m_Games[gameMode];
            m_CurrentGame.Initialize();
            GameEntry.Gamefacade.InitData();
        }

        private void OnChangeScene(object sender, GameEventArgs e)
        {
            CustomBaseEventArgs cbe = (CustomBaseEventArgs)e;
            if(cbe.EventName=="ChangeScene")
            {
                ChangeSceneName = (string)cbe._Values[0];
                if (ChangeSceneName.Equals("Load"))
                    LoadSceneName = (string)cbe._Values[1];
                //battleName= cbe._Values.Length==2? (string)cbe._Values[1]:"";
                //GameEntry.Event.Fire(this, CustomBaseEventArgs.Create("MoveAnimationStop", "Int-State-0"));
            }
        }

        protected override void OnLeave(ProcedureOwner procedureOwner, bool isShutdown)
        {
            if (m_CurrentGame != null)
            {
                m_CurrentGame.Shutdown();
                m_CurrentGame = null;
            }
            GameEntry.Event.Unsubscribe(CustomBaseEventArgs.EventId, OnChangeScene);
            base.OnLeave(procedureOwner, isShutdown);
        }

        protected override void OnUpdate(ProcedureOwner procedureOwner, float elapseSeconds, float realElapseSeconds)
        {
            base.OnUpdate(procedureOwner, elapseSeconds, realElapseSeconds);
            if (!ChangeSceneName.IsNullOrEmpty())
            {
                procedureOwner.SetData<VarString>("NextSceneId", ChangeSceneName);
                if (ChangeSceneName.Equals("Load"))
                {
                    procedureOwner.SetData<VarString>("LoadScene", LoadSceneName);
                }
                //procedureOwner.SetData<VarString>("BattleName", battleName);
                ChangeState<ProcedureChangeScene>(procedureOwner);
            }
            if (m_CurrentGame != null && !m_CurrentGame.GameOver)
            {
                m_CurrentGame.Update(elapseSeconds, realElapseSeconds);
                return;
            }

            //if (ChangeSceneName.IsNullOrEmpty())
            //{
            //    //ChangeScene = true;
            //    m_GotoMenuDelaySeconds = 0;
            //}

            //m_GotoMenuDelaySeconds += elapseSeconds;
            //if (m_GotoMenuDelaySeconds >= GameOverDelayedSeconds)
            //{
            //    procedureOwner.SetData<VarInt32>("NextSceneId", GameEntry.Config.GetInt("Scene.Menu"));
            //    ChangeState<ProcedureChangeScene>(procedureOwner);
            //}
        }
    }
}
